package com.dq.domain.orders;

import com.dq.vo.baseVo.VO;

import com.dq.domain.orders.info.OrderInfo;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;
import java.util.List;


/**
 * @author liuhui
 * @date 2019/9/12 0012 14:06:21
 */
@Data
@Entity
@ApiModel("销售单")
public class UserOrder extends Orders implements VO.ToVO {

    @ApiModelProperty("关联顾客")
    @ManyToOne
    protected SysUser wxUser;

    @ApiModelProperty("订单原价")
    protected BigDecimal originalPrice;
    @ApiModelProperty("优惠金额")
    protected BigDecimal discountPrice;
    @ApiModelProperty("欠款金额")
    protected BigDecimal arrearsPrice;
    @ApiModelProperty("是否欠款, 检索")
    protected Boolean isOwed;
    @ApiModelProperty("核销时间")
    protected Long checkoutTime;
    @ApiModelProperty("核销二维码")
    protected String checkOutQrUrl;
    @ApiModelProperty("订单来源(线下订单,线上订单,积分订单,活动订单)")
    @Enumerated(EnumType.ORDINAL)
    protected OrderSource orderSource;
    @ApiModelProperty("订单状态")
    @Enumerated(EnumType.ORDINAL)
    protected UserOrderStatus status;
    @ApiModelProperty("支付方式")
    @Enumerated(EnumType.ORDINAL)
    protected PayType payType;
    @ApiModelProperty("外部支付订单号")
    protected String outTradeNo;
    @ApiModelProperty("外部退款订单号")
    protected String outRefundNo;
    @ApiModelProperty("赠送项目")
    protected String giveProject;
    @ApiModelProperty("商品件数（购买总数目）")
    protected Long payGoodsNum;
    @ApiModelProperty("商品品种数")
    protected Long cateNum;
    @ApiModelProperty("用户积分余额")
    protected Long integralBalance;
    @ApiModelProperty("是否结算")
    protected Boolean isSettle;

    public enum PayType {
        alipay("支付宝"),
        wxpay("微信"),
        card("刷卡支付"),
        cash("现金支付");

        public String name;

        PayType(String name) {
            this.name = name;
        }
    }
    public enum UserOrderStatus {
        waitPay("待支付"),
        waitCheckOut("待核销"),
        alreadyCheckOut("已核销"),
        alreadyCancel("已取消"),
        alreadyGetGoods("已收货"),
        orderShutDown("订单关闭"),
        returnIng("退货中"),
        returnOver("退货完成")
        ;
        public String name;

        UserOrderStatus(String name) {
            this.name = name;
        }
    }

    public enum OrderSource{
        onLine("线上"),underLine("线下"), integral("积分"), activity("活动");
        public String name;

        OrderSource(String name){
            this.name = name;
        }
    }
    public UserOrder(String orderNo, String remark, Org org, SysUser user, Long ordersPayCountDownTime, Long payGoodsNum, Long cateNum) {
        super(orderNo,remark, org,ordersPayCountDownTime);
        this.wxUser = user;
        this.status = UserOrderStatus.waitPay;
        this.payGoodsNum = payGoodsNum;
        this.cateNum = cateNum;
    }
    public UserOrder(String orderNo, String remark, Org org, SysUser user, Long ordersPayCountDownTime,String giveProject,Long num,Boolean flag,BigDecimal arrearsPrice,Long payGoodsNum,Long cateNum,BigDecimal sumPrice,BigDecimal discountPrice) {
        super(orderNo,remark,org,ordersPayCountDownTime,sumPrice.subtract(discountPrice));
        this.wxUser = user;
        this.giveProject = giveProject;
        this.status = UserOrderStatus.alreadyCheckOut;
        this.integralBalance = num;
        this.isOwed = flag;
        this.arrearsPrice = arrearsPrice;
        this.payGoodsNum = payGoodsNum;
        this.cateNum = cateNum;
        this.originalPrice = sumPrice;
        this.discountPrice = discountPrice;
    }

    public UserOrder() {

    }

    public void amountAdd(BigDecimal amount) { // 累计金额
        this.originalPrice = fmt(this.originalPrice).add(amount);
        this.price = this.originalPrice;
    }

    @Data
    @ApiModel(value = "用户订单视图")
    public static class Vo implements VO {
        @ApiModelProperty("订单id")
        private Long id;
        @ApiModelProperty("订单号")
        private String ordersNo;
        private String createTime;
        @ApiModelProperty("订单状态")
        private String status;
        @ApiModelProperty(value = "机构", hidden = true)
        private Org.Vo org;
        @ApiModelProperty(value = "用户", hidden = true)
        private VO user;
        @ApiModelProperty("核销二维码")
        private String checkOutQrUrl;
        @ApiModelProperty("支付方式")
        private String payType;
        @ApiModelProperty("订单超时时间")
        private String ordersPayCountDownTime;
        @ApiModelProperty("订单原价")
        private BigDecimal originalPrice;
        @ApiModelProperty("优惠金额")
        private BigDecimal discountPrice;
        @ApiModelProperty("最终支付金额")
        private BigDecimal payPrice;
        @ApiModelProperty("外部订单号")
        private String outTradeNo;
        @ApiModelProperty("付款时间")
        private String paySuccessTime;
        @ApiModelProperty("交易完成时间")
        private String tradeSuccessTime;
        @ApiModelProperty("留言")
        private String remark;
        @ApiModelProperty("门店名")
        private String orgName;
        @ApiModelProperty("会员昵称")
        private String localNickName;
        @ApiModelProperty("会员昵称")
        private String cardNo;
        @ApiModelProperty("会员手机号")
        private String mobile;
        @ApiModelProperty("orderInfo")
        private List<OrderInfo.Vo> orderInfoVos;
        @ApiModelProperty("订单状态en")
        protected UserOrderStatus statusEn;
    }

    @Data
    @ApiModel("门店端消费记录视图")
    public static class OrgSaleListVo{
        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("订单号")
        protected String ordersNo;
        @ApiModelProperty("销售金额")
        protected BigDecimal saleAmount;
        @ApiModelProperty("优惠金额")
        protected BigDecimal discountPrice;
        @ApiModelProperty("欠款金额")
        protected BigDecimal arrearsPrice;
        @ApiModelProperty("实际支付")
        protected BigDecimal payPrice;
        @ApiModelProperty("销售时间")
        protected String saleTime;
        @ApiModelProperty("状态")
        protected String status;
        @ApiModelProperty("状态en")
        protected UserOrderStatus statusEN;
        @ApiModelProperty("销售来源")
        protected String orderSource;
        @ApiModelProperty("销售来源EN")
        protected OrderSource orderSourceEN;
        @ApiModelProperty("商品件数（购买总数）")
        protected Long payGoodsNum;
        @ApiModelProperty("分类数（商品数）")
        protected Long cateNum;
        @ApiModelProperty("订单所属门店")
        protected String orgName;
        @ApiModelProperty("订单详情")
        protected List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos;
    }
    @Data
    @ApiModel("basic")
    public static class BasicVo{
        @ApiModelProperty("关联顾客")
        protected Long wxUserId;
        @ApiModelProperty("关联顾客")
        protected String wxUserRealcName;
        @ApiModelProperty("门店")
        protected Long orgId;
        @ApiModelProperty("门店")
        protected String orgName;

        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("订单号")
        protected String ordersNo;
        @ApiModelProperty("订单原价")
        protected BigDecimal originalPrice;
        @ApiModelProperty("优惠金额")
        protected BigDecimal discountPrice;
        @ApiModelProperty("最终支付金额")
        protected BigDecimal price;
        @ApiModelProperty("欠款金额")
        protected BigDecimal arrearsPrice;
        @ApiModelProperty("是否欠款")
        protected Boolean isOwed;
        @ApiModelProperty("核销时间")
        protected String checkoutTime;
        @ApiModelProperty("创建时间")
        protected String createTime;
        @ApiModelProperty("支付时间")
        protected String paySuccessTime;
        @ApiModelProperty("订单来源(线下订单,线上订单,积分订单,活动订单)")
        protected String orderSourceEn;
        @ApiModelProperty("订单来源(线下订单,线上订单,积分订单,活动订单)")
        protected String orderSourceCn;
        @ApiModelProperty("订单状态")
        protected String statusEn;
        @ApiModelProperty("订单状态")
        protected String statusCn;
        @ApiModelProperty("支付方式")
        protected String payTypeEn;
        @ApiModelProperty("支付方式")
        protected String payTypeCn;
        @ApiModelProperty("商品件数（购买商品总数）")
        protected Long payGoodsNum;
        @ApiModelProperty("分类数（商品数）")
        protected Long cateNum;
        @ApiModelProperty("订单详情")
        protected List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos;
        @ApiModelProperty("关联员工")
        protected List<StaffUser.Vo> staffUserList;
        @ApiModelProperty("是否分配")
        protected Boolean isDistribution;
    }
    public BasicVo toBasicVo(){
        UserOrder.BasicVo vo = new UserOrder.BasicVo();
        vo.wxUserId = this.getWxUser() == null ? null : this.getWxUser().getId();
        vo.wxUserRealcName = this.getWxUser() == null ? null : this.getWxUser().getUserRealcName();
        vo.orgId = this.getOrg() == null ? null : this.getOrg().getId();
        vo.orgName = this.getOrg() == null ? null : this.getOrg().getSimpleName();

        vo.id = this.getId();
        vo.ordersNo = this.getOrdersNo();
        vo.originalPrice = fmt(this.getOriginalPrice());
        vo.discountPrice = fmt(this.getDiscountPrice());
        vo.price = fmt(this.getPrice());
        vo.arrearsPrice = fmt(this.getArrearsPrice());
        vo.isOwed = this.getIsOwed();
        vo.checkoutTime = fmt(this.getCheckoutTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.paySuccessTime = fmt(this.getPaySuccessTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);

        vo.orderSourceEn =this.getOrderSource()!=null? this.getOrderSource().toString():"";
        vo.orderSourceCn = this.getOrderSource()!=null? this.getOrderSource().name:"";
        vo.statusEn = this.getStatus().toString();
        vo.statusCn = this.getStatus().name;
        vo.payTypeEn = this.getPayType()!= null ?this.getPayType().toString():"";
        vo.payTypeCn = this.getPayType()!= null ?this.getPayType().name:"";
        vo.payGoodsNum = fmt(this.getPayGoodsNum());
        vo.cateNum = this.getCateNum();
        vo.isDistribution = this.getIsDistribution();
        return vo;
    }

    public OrgSaleListVo toOrgSaleListVo(){
        UserOrder.OrgSaleListVo orgSaleListVo = new UserOrder.OrgSaleListVo();
        orgSaleListVo.id = this.getId();
        orgSaleListVo.ordersNo = fmt(this.getOrdersNo());
        orgSaleListVo.saleAmount = fmt(this.getOriginalPrice());
        orgSaleListVo.discountPrice = fmt(this.getDiscountPrice());
        orgSaleListVo.arrearsPrice = fmt(this.getArrearsPrice());
        orgSaleListVo.payPrice = fmt(this.getPrice());
        orgSaleListVo.saleTime = fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        orgSaleListVo.status = fmt(this.getStatus().name);
        orgSaleListVo.statusEN=this.getStatus();
        orgSaleListVo.orderSource =this.getOrderSource() != null?fmt(this.getOrderSource().name):null;
        orgSaleListVo.orderSourceEN=this.getOrderSource() != null?this.getOrderSource():null;
        orgSaleListVo.payGoodsNum = fmt(this.getPayGoodsNum());
        orgSaleListVo.cateNum = fmt(this.getCateNum());
        orgSaleListVo.orgName = this.getOrg()!=null ?fmt(this.getOrg().getSimpleName()):"";
        return orgSaleListVo;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();

        return vo;
    }
    public VO toVO(List<OrderInfo.Vo> orderInfoVos) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.ordersNo = this.getOrdersNo();
        vo.status = this.getStatus().name;
        vo.checkOutQrUrl = this.checkOutQrUrl;
        vo.payType = this.getPayType() != null ? this.getPayType().name : "";
        vo.ordersPayCountDownTime = fmt(this.getOrdersPayCountDownTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.originalPrice = fmt(this.getOriginalPrice());
        vo.discountPrice = fmt(this.getDiscountPrice());
        vo.payPrice = fmt(this.getPrice());
        vo.outTradeNo = fmt(this.getOutTradeNo());
        vo.paySuccessTime = fmt(this.getPaySuccessTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.tradeSuccessTime = fmt(this.getTradeSuccessTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.remark = fmt(this.getRemark());
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.orderInfoVos = orderInfoVos;
        vo.localNickName = this.wxUser != null ? fmt(this.wxUser.getUserRealcName()) : "";
        vo.cardNo = this.wxUser != null ? this.wxUser.getCardNo() : "";
        vo.mobile = this.wxUser != null ? fmt(this.wxUser.getMobile()) : "";
        vo.statusEn = this.status;
        return vo;
    }
}
